package com.smart.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.campus.dao.SysUserMapper;
import com.smart.campus.dto.SysUser;
import com.smart.campus.pojo.PagedResponse;
import com.smart.campus.service.SysUserService;
import com.smart.campus.util.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils.eq;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysUser> findUserList(SysUser sysUser) {
        return sysUserMapper.selectList(null);
    }

    @Override
    public PagedResponse<SysUser> pageUser(SysUser sysUser, Integer pageNum, Integer pageSize) {
        Page<SysUser> page = new Page<>(pageNum, pageSize);
        IPage<SysUser> sysUserIPage = sysUserMapper.pageUser(sysUser,page);
        return PaginationUtil.getPaginationResponse(sysUserIPage.getRecords(),pageNum,pageSize,sysUserIPage.getTotal());
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
    public void addUser(SysUser sysUser) {
        sysUser.setCreateTime(new Date());
        sysUser.setUpdateTime(new Date());
        sysUserMapper.insert(sysUser);

        Map<String,Long> map = new HashMap<>();
        map.put("userId",sysUser.getUserId());
        map.put("deptId",sysUser.getDeptId());
        sysUserMapper.insertUserIdAndDeptId(map);


        Map<String,Long> map1 = new HashMap<>();
        map1.put("userId",sysUser.getUserId());
        map1.put("roleId",sysUser.getRoleId());
        sysUserMapper.insertUserIdAndRoleId(map1);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void delUser(Long userId) {
        sysUserMapper.deleteById(userId);
        sysUserMapper.deleteUserIdAndDeptId(userId);
        sysUserMapper.deleteUserIdAndRoleId(userId);
    }

    @Override
    public SysUser findUserById(Long userId) {
        return sysUserMapper.selectById(userId);
    }

    @Override
    public void updateUser(SysUser sysUser) {
        sysUserMapper.updateById(sysUser);
    }

    @Override
    public SysUser findSysUserByUserName(String username) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", username);

        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public PagedResponse<SysUser> findAll(SysUser sysUser, Integer pageNum, Integer pageSize) {
        Page<SysUser> page = new Page<>(pageNum, pageSize);
        IPage<SysUser> page1 = sysUserMapper.findAll(sysUser, page);
        return PaginationUtil.getPaginationResponse(page1.getRecords(),pageNum,pageSize,page1.getTotal());
    }

    @Override
    public List<SysUser> getTeachersList() {
        return sysUserMapper.getTeachersList();
    }

    @Override
    public void updateStatus(SysUser sysUser) {
        sysUserMapper.updateById(sysUser);
    }

    @Override
    public List<SysUser> getDetails(SysUser sysUser) {
        return sysUserMapper.getDetails(sysUser);
    }
}
